package com.approval.approvalapp.index.dbsx.qjsp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.QjBean;
import com.approval.approvalapp.bean.QjDbBean;
import com.approval.approvalapp.index.dbsx.qjsp.QjSpActivity;
import com.approval.approvalapp.index.dbsx.qjsp.QjspDbXqActivity;
import com.approval.approvalapp.util.SpUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by louyulin on 2019/4/28.
 */

public class QjDbAdapter extends RecyclerView.Adapter<QjDbAdapter.Holder> {

    List<QjBean.StayBean> listBeans;

    Context context;

    public void setListBeans(List<QjBean.StayBean> listBeans) {
        this.listBeans = listBeans;
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_qjspdb, parent, false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        holder.tv1.setText(listBeans.get(position).getTitle());


        Long timeStamp = new Long(listBeans.get(position).getAdd_time()) * 1000;  //获取当前时间戳
        Date date = new Date(timeStamp);
        int dateresult = date.getYear() + 1900;
        Date date2 = new Date(System.currentTimeMillis());
        int currentResult = date2.getYear() + 1900;

        if (dateresult == currentResult) {
            //同年
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
            String timese = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
            holder.tv2.setText(timese);
        } else {
            //不同年
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM-dd HH:mm");
            String timese = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
            holder.tv2.setText(timese);
        }

        holder.tv3.setText("请假类型: " + listBeans.get(position).getType());
        holder.tv4.setText("开始时间: " + listBeans.get(position).getStart());
        holder.tv5.setText("结束时间: " + listBeans.get(position).getEnd());
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, QjspDbXqActivity.class);
                intent.putExtra("qjlx", listBeans.get(position).getType());
                intent.putExtra("id", listBeans.get(position).getId());
                if (SpUtil.getString(context, SpUtil.Usertype, "").equals("1")
                        && QjSpActivity.instance.uriid.equals("1")) {
                    intent.putExtra("qtgzfw", listBeans.get(position).getQt_gzfw());
                }
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (listBeans == null || listBeans.size() == 0) {
            return 0;
        } else {
            return listBeans.size();
        }
    }

    class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv1)
        TextView tv1;
        @Bind(R.id.tv2)
        TextView tv2;
        @Bind(R.id.tv3)
        TextView tv3;
        @Bind(R.id.tv4)
        TextView tv4;
        @Bind(R.id.tv5)
        TextView tv5;
        @Bind(R.id.root)
        LinearLayout root;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
